//
//  XGHomeViewController.m
//  XGNotifyJump
//
//  Created by 小果 on 16/8/22.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGHomeViewController.h"

#define fontSize 35
#define buyCarName @"老公：我要买车"
#define redPakectName @"老婆：我要抢红包"
#define deleteNotiName @"删除所有通知"

@interface XGHomeViewController ()
@property (nonatomic, weak) UIButton *buyCar;
@property (nonatomic, weak) UIButton *redPacket;
@property (nonatomic, weak) UIButton *deleteNoti;
@end

@implementation XGHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UIButton *buyCar = [[UIButton alloc] init];
    self.buyCar = buyCar;
    [self btnWith:buyCar TextName:buyCarName textColor:[UIColor redColor] textFontSize:fontSize];
    
    UIButton *redPacket = [[UIButton alloc] init];
    self.redPacket = redPacket;
    [self btnWith:redPacket TextName:redPakectName textColor:[UIColor blueColor] textFontSize:fontSize];
    
    UIButton *deleteNoti = [[UIButton alloc] init];
    self.deleteNoti = deleteNoti;
    [self btnWith:deleteNoti TextName:deleteNotiName textColor:[UIColor magentaColor] textFontSize:fontSize];
    
}
#pragma mark - 公有方法
-(void)btnWith:(UIButton *)btn TextName:(NSString *)name textColor:(UIColor *)color textFontSize:(CGFloat)Size{
    [btn setTitle:name forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:Size];
    [btn addTarget:self action:@selector(buyCarAndRedPacketClick:) forControlEvents:UIControlEventTouchUpInside];
    CGSize btnSize = [btn.titleLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:fontSize] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    [self.view addSubview:btn];
    if ([name isEqualToString:buyCarName]){
        [btn setTitleColor:color forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuide).offset(100);
            make.centerX.mas_equalTo(self.view);
            make.size.mas_equalTo(btnSize);
        }];
    }else if ([name isEqualToString:redPakectName]){
        [btn setTitleColor:color forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.buyCar.mas_bottom).offset(40);
            make.centerX.equalTo(self.buyCar);
            make.size.mas_equalTo(btnSize);
        }];
    }else{
        [btn setTitleColor:color forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.redPacket.mas_bottom).offset(40);
            make.centerX.equalTo(self.redPacket);
            make.size.mas_equalTo(btnSize);
        }];
    }
    
}
#pragma mark - 接收通知后跳转到指定页面
-(void)buyCarAndRedPacketClick:(UIButton *)btn{
    
    if ([btn.titleLabel.text isEqualToString:buyCarName]){
        
        [self localNotificationWithBody:@"老公: 我要买世上最贵的车了" userInfo:@{@"userName": @"老公", @"selectIndex" : @(1)}];
        
    }else if ([btn.titleLabel.text isEqualToString:redPakectName]){
        [self localNotificationWithBody:@"老婆: 我要开开心心的抢红包" userInfo:@{@"userName": @"老婆", @"selectIndex" : @(2)}];
    }else{
        
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    }
}

#pragma mark - 公用加添通知方法
- (void)localNotificationWithBody:(NSString *)alertBody userInfo:(NSDictionary *)userInfo
{
    //1. 创建本地推送通知
    UILocalNotification *local = [UILocalNotification new];
    
    //2. 设置属性
    // 2.0 设置滑动解锁及提醒样式的文字
    local.alertAction = @"哈哈哈哈";
    // 如果设置NO，提醒样式变为“打开”，滑动解锁就会变成“滑动来查看”
    //    local.hasAction = NO;
    
    
    //2.1 触发时间 --5秒后
    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
    
    //2.2 设置重复——>最小单位是分钟
    //    local.repeatInterval = NSCalendarUnitMinute;
    
    //2.3 设置重复所参考的日历(中国的农历有时时13个月)
    //    local.repeatCalendar = [NSCalendar calendarWithIdentifier:@"NSCalendarIdentifierChinese"];
    
    //2.4 内容
    local.alertBody = alertBody;
    
    //2.5 用户参数
    local.userInfo = userInfo;
    
    //2.6 设置分类
    local.category = @"category";
    
    //2.7 应用图标的数字
    local.applicationIconBadgeNumber = 1;
    
    
    //3. 添加到调度池 --> 使用UIApplication调度 --> iOS7到这一步就OK了
    [[UIApplication sharedApplication] scheduleLocalNotification:local];
    
    // 注：如果发送多个推送, 推送的注册只需要一次, 因此可以直接放在AppADelegate中.
}

@end
